package com.tzbank.poc.serviceimpl;

import com.tzbank.poc.dao.AccountBalanceMapper;
import com.tzbank.poc.dao.BindBankCardMapper;
import com.tzbank.poc.dao.CustomerMapper;
import com.tzbank.poc.model.AccountBalance;
import com.tzbank.poc.model.BindBankCard;
import com.tzbank.poc.model.BindBankCardKey;
import com.tzbank.poc.model.Customer;
import com.tzbank.poc.service.CustomerService;
import com.tzbank.poc.util.IdUtil;
import com.tzbank.poc.util.ReturnMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author li_zheng2
 * Create by li_zheng2 on 2019/1/21.
 */
@Service(value = "customerService")
public class CustomerServiceImpl implements CustomerService {

    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private BindBankCardMapper bankCardMapper;

    @Autowired
    private AccountBalanceMapper balanceMapper;

    @Override
    public int addCustomer(@ModelAttribute Customer customer) {
        return customerMapper.insertSelective(customer);
    }

//    @Override
//    public Customer queryCustomer(Customer customer) {
//        return customerMapper.se(customer);
//    }

    @Override
    public Map<String, Object> queryCustomerInfo(String userNo) {
        Customer customer = customerMapper.selectByPrimaryKey(userNo);
        Map customerMap = new HashMap();
        Class cu = customer.getClass();
        Field[] fields = cu.getDeclaredFields();
        Arrays.stream(fields).forEach(field -> {
            boolean flag = field.isAccessible();
            field.setAccessible(true);
            try {
                customerMap.put(field.getName(), field.get(customer));
            } catch (IllegalAccessException e) {
                ReturnMapUtil.returnFail(e.getMessage());
            }
            field.setAccessible(flag);
        });

        return customerMap;
    }

    @Override
    public Map<String, Object> queryCustomerByPhoneNo(String phoneNo) {
        return customerMapper.selectByPhoneNo(phoneNo);
    }

    @Override
    public int updateCustomer(Customer customer) {
        return customerMapper.updateByPrimaryKeySelective(customer);
    }

    @Override
    public Map<String, Object> openAccountBindCard(BindBankCard bindBankCard) {
        String userNo = bindBankCard.getUserNo();
        Customer user = customerMapper.selectByPrimaryKey(userNo);

        Map map = new HashMap();
        if(StringUtils.isEmpty(user.getAccountNo()) ){
            String accountNo = IdUtil.getAccountId(userNo);
            map.put("accountNo",accountNo);
            Customer customer = new Customer();
            customer.setUserNo(userNo);
            customer.setAccountNo(accountNo);
            customerMapper.updateByPrimaryKeySelective(customer);

            //登记余额
            AccountBalance accountBalance = new AccountBalance();
            accountBalance.setUserNo(userNo);
            accountBalance.setAccountNo(accountNo);
            accountBalance.setAccountBalance(new BigDecimal(0));
            balanceMapper.insertSelective(accountBalance);
        }else{
            map.put("accountNo",user.getAccountNo());
        }
        ReturnMapUtil.setBodyMap(map);
        try {
            bankCardMapper.insertSelective(bindBankCard);
        }catch (Exception e){
            return ReturnMapUtil.returnErrorMap(null,null,"此卡已绑");
        }

        return ReturnMapUtil.getSuccessMap();
    }

    @Override
    public int deleteBindCard(BindBankCardKey bindBankCardKey) {
        return bankCardMapper.deleteByPrimaryKey(bindBankCardKey);
    }

    @Override
    public List<BindBankCard> queryBindCardList(String userNo) {
        return bankCardMapper.selectByUserNo(userNo);
    }


}
